#include <iostream>
using namespace std;

//单例模式: 饿汉式
class Singleton {
public:
	//静态的成员函数
	static Singleton& getInstance(void) {
		return s_instance;
	}
	void print (void) {
		cout << m_data << endl;
	}
private:
	int m_data;
	//1) 私有的构造函数
	Singleton (int data = 0):m_data(data) {
		cout << "单例对象被创建了" << endl;
	};
	//2) 静态成员变量维护唯一的对象
	static Singleton s_instance;
};

Singleton Singleton::s_instance (1234);

int main (void)
{
	//禁止在类的外部创建对象
	//Singleton s;
	//Singleton *ps = new Singleton;
	Singleton& s1 = Singleton::getInstance ();
	Singleton& s2 = Singleton::getInstance ();
	Singleton& s3 = Singleton::getInstance ();
	s1.print();
	s2.print();
	s3.print();
	cout << &s1 << endl;
	cout << &s2 << endl;
	cout << &s3 << endl;
	return 0;
}
